function getCategoryList() {
    http.ajax({
        type: 'get',
        url: '/admin/category/list',
        success: (res) => {
            // console.log(res);
            const readerRes = template('artcleTemplate', res);
            $('tbody').html(readerRes);
        }
    })
}

//删除文章类别
function delCateById(id) {
    http.ajax({
        type: 'post',
        url: '/admin/category/delete',
        data: { id },
        success: (res) => {
            // console.log(res);
            if (res.code === 204) {
                getCategoryList();
            }
        }
    })
}

//编辑文章类别
//定义一个变量，记录当前编辑的ID
let editId;
function editCategory(item) {
    $('#editCate').show();
    $('#name').val(item.name);
    $('#slug').val(item.slug);
    editId = item.id;
}

//取消编辑
function hideEditCategory() {
    $('#editCate').hide();
}

//保存编辑
function saveEditCategory() {
    const name = $('#name').val();
    const slug = $('#slug').val();
    const id = editId;
    http.ajax({
        type: 'post',
        url: '/admin/category/edit',
        data: { name, slug, id },
        success: (res) => {
            // console.log(res);
            hideEditCategory();
            getCategoryList();
        }
    })
}

//增加文章类别
function addcategory() {
    $('#addCate').show()
}

//保存增加的文章类别
function saveAddCategory() {
    const name = $('#name_new').val();
    const slug = $('#slug_new').val();
    http.ajax({
        type: 'post',
        url: '/admin/category/add',
        data: { name, slug },
        success: (res) => {
            hideAddCategory();
            getCategoryList();
        }
    })
}

//取消增加
function hideAddCategory() {
    $('#addCate').hide();
}

getCategoryList();